package com.example.baidumap_a.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.baidumap_a.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class orderFragment extends Fragment {
    private TabLayout tabLayout;
    private View view;
    private ViewPager viewPager;
    private List<Fragment> listFragment;
    private List<String> listTitle;
    private FragmentPagerAdapter mAdapter;
    private String name;
    private orderFgListener orderFgListener;
    private ChargeFragment chargeFragment1, chargeFragment2, chargeFragment3;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        orderFgListener = (orderFragment.orderFgListener) context;
    }

    public orderFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        name = orderFgListener.orderGetUsername();
        System.out.println("onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_order, container, false);
        viewPager = view.findViewById(R.id.vp_order);
        tabLayout = view.findViewById(R.id.tab_layout_order);
        listFragment = new ArrayList<>(3);
        listTitle = new ArrayList<>(3);
        listTitle.add("车位订单");
        listFragment.add(new ParkOrderFragment(name));
        listTitle.add("支出订单");
        listFragment.add(new ChargeFragment(name,2));
        listTitle.add("充值订单");
        listFragment.add(new ChargeFragment(name,1));
        mAdapter = new FragmentAdapter(getChildFragmentManager(), listFragment, listTitle);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(1);
        tabLayout.setupWithViewPager(viewPager);//与ViewPage建立关系

        return view;
    }

    private class FragmentAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragmentList;//各导航的Fragment
        private List<String> mTitle; //导航的标题
        public FragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> title) {
            super(fragmentManager);
            mFragmentList = fragments;
            mTitle = title;
        }
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }
        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle.get(position);
        }
    }


    public interface orderFgListener {
        String orderGetUsername();
    }

    @Override
    public void onPause() {
        super.onPause();

    }


}

